package ee.ut.cs.mc.madp.p2pdivider.fragment;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;

import android.net.wifi.p2p.WifiP2pDevice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTitleStrip;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ee.ut.cs.mc.madp.p2pdivider.BaseDividerActivity;
import ee.ut.cs.mc.madp.p2pdivider.R;
import ee.ut.cs.mc.madp.p2pdivider.Utils;

/**
 * @author Kaarel Jõgeva
 *
 */
public class PlayFragment extends Fragment {

    final private static String TAG = PlayFragment.class.getSimpleName();

    public PlayFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_play, null);

        /*
         * PagerTitleStrip titleStrip = (PagerTitleStrip) getActivity().findViewById(R.id.pager_title_strip);
         * titleStrip.setAlpha(0F);
         * try {
         * // Get display size
         * DisplayMetrics metrics = new DisplayMetrics();
         * getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
         * int w = metrics.widthPixels;
         * int h = metrics.heightPixels;
         * DividedVideoView videoView = (DividedVideoView) view.findViewById(R.id.videoView);
         * File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
         * File video = new File(dcim.getAbsolutePath() + "/" + "documentariesandyou.mp4");
         * MediaController mc = new MediaController(getActivity());
         * mc.setAnchorView(videoView);
         * videoView.setMediaController(mc);
         * Uri videoUri = Uri.fromFile(video);
         * videoView.setVideoURI(videoUri);
         * } catch (Exception e) {
         * Log.e(TAG, "JERRRROR!", e);
         * }
         */
        return view;
    }

    @Override
    public void onDestroyView() {
        PagerTitleStrip titleStrip = (PagerTitleStrip) getActivity().findViewById(R.id.pager_title_strip);
        titleStrip.setAlpha(1F);
        super.onDestroyView();
    }

    public class ClientThread implements Runnable {

        private final WifiP2pDevice remote;
        private final List<File> files;

        public ClientThread(WifiP2pDevice wifiP2pDevice, List<File> files) {
            remote = wifiP2pDevice;
            this.files = files;
        }

        @Override
        public void run() {
            try {
                Log.d("ClientActivity", "C: Connecting to " + remote.deviceName);

                String ipFromMac = "192.168.49.1";// getIPFromMac(remote.deviceAddress);

                for (File file : files) {
                    InetAddress byName = InetAddress.getByName(ipFromMac);
                    Socket socket = new Socket(byName, BaseDividerActivity.SERVERPORT);
                    OutputStream os = null;
                    FileInputStream fis = null;
                    try {
                        os = socket.getOutputStream();
                        // Utils.writeCommand(os, Command.FILE);
                        fis = new FileInputStream(file);
                        String name = file.getName();
                        Log.d(TAG, "Sending file name " + name);
                        for (char c : name.toCharArray()) {
                            os.write(c);
                        }
                        os.write('|');
                        Utils.copyFile(fis, os);
                    } catch (Exception e) {
                        Log.e(TAG, "S: Error", e);
                    } finally {
                        Utils.closeQuietly(os, fis);
                    }
                    socket.close();
                    Log.d(TAG, "C: Closed.");
                }
            } catch (Exception e) {
                Log.e(TAG, "C: Error", e);
            }
        }
    }
}
